package com.jlkf.common.redis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.jlkf.common.redis.service.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisClientSingle implements JedisClient{
	
	@Autowired
	private JedisPool jedisPool; 
	
	@Override
	public String get(String key) {
		Jedis jedis =null;
		String string =null;
		try {
			 jedis = jedisPool.getResource();
			/*jedis.auth("fabtech");*/
			 string = jedis.get(key);
			jedis.close();
			return string;
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
	            e.printStackTrace();
		}finally {
            //返还到连接池
            returnResource(jedisPool, jedis);
        }
		return key;
	}
	
	public static void returnResource(JedisPool pool, Jedis redis) {
        if (redis != null) {
            pool.returnResource(redis);
        }
    }

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		jedis.auth("fabtech");
		String string = jedis.set(key, value);
		jedis.close();
		return string;
	}

	@Override
	public String hget(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		/*jedis.auth("fabtech");*/
		String string = jedis.hget(hkey, key);
		jedis.close();
		return string;
	}

	@Override
	public long hset(String hkey, String key, String value) {
		Jedis jedis = jedisPool.getResource();
		/*jedis.auth("fabtech");*/
		Long result = jedis.hset(hkey, key, value);
		jedis.close();
		return result;
	}

	@Override
	public long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		/*jedis.auth("fabtech");*/
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	@Override
	public long expire(String key, int second) {
		Jedis jedis = jedisPool.getResource();
		/*jedis.auth("fabtech");*/
		Long result = jedis.expire(key, second);
		jedis.close();
		return result;
	}

	@Override
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		/*jedis.auth("fabtech");*/
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

}

